package com.example.demo.nyxcosmetics;

import org.apache.commons.collections4.CollectionUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MergeExportDataUtils {
    public static List<ExportDateExt> merge(ExportDate data) {
        ExportDateExt template = new ExportDateExt();
        BeanUtils.copyProperties(data, template);

        if (CollectionUtils.isEmpty(data.getProducts())) {
            return Collections.singletonList(template);
        }

        List<ExportDateExt> list = new ArrayList<>();
        for (int i = 0; i < data.getProducts().size(); i++) {
            ExportDateExt ext = getExportDateExt(data, i, template);

            list.add(ext);
        }
        return list;
    }

    @NotNull
    private static ExportDateExt getExportDateExt(ExportDate data, int i, ExportDateExt template) {
        ExportDateExt ext = new ExportDateExt();
        if (i == 0) {
            ext = template;
        }
        ExportDate product = data.getProducts().get(i);

        ext.setProduct_name(product.getName());
        ext.setProduct_subName(product.getSubName());
        ext.setProduct_category(product.getCategory());
        ext.setProduct_price(product.getPrice());
        ext.setProduct_salePrice(product.getSalePrice());
        ext.setProduct_currency(product.getCurrency());
        ext.setProduct_stock(product.getStock());
        ext.setProduct_image(product.getImage());
        return ext;
    }
}
